/**
 * http函数专门发送请求
 * @param {} param0 
 * @returns 
 * http({method="post"})
 * http()
 * send()
 */
export const baseURL = "http://local.wfh183943843.com"
export default function http({url,method="GET",data}={}){
    return new Promise((resolve,reject)=>{
        $.ajax({
            url:baseURL+url,
            method,
            headers:{
                token:localStorage.token,  //localStorage.token等于localStorage.getItem("token")
            },
            dataType:"JSON",
            data,
            success(msg){
                if (msg.code==1) {
                    resolve(msg)
                }else{
                    reject(msg.msg)
                }
                // resolve(msg)
            },
            error(err){
                console.log("请求失败",err);
                //前端统一处理后端异常
                if(err.status == 401){
                    localStorage.removeItem("token")
                    localStorage.removeItem("user_info")
                    window.location.assign("/index.html")
                }else if(err.status == 500){
                    //跳转到一个页面，服务器开小差
                }
                reject(err)
            }
        })
    })

}



